#include "ET++.ph"
#ifdef __GNUG__
#pragma implementation
#endif

#include "Buttons.h"
#include "BorderItems.h"
#include "Scroller.h"
#include "CollView.h"
#include "Window.h"
#include "Box.h"
#include "OrdColl.h"
#include "Class.h"
#include "String.h"

#include "InspClickDialog.h"

#include "ClassItem.h"

//---- PeInspectClickDialog ---------------------------------------------------------

NewMetaImpl(PeInspectClickDialog, Dialog, (TP(objList)));

PeInspectClickDialog::PeInspectClickDialog() : Dialog("Inspect Click")
{
	objList= new CollectionView(this, 0, eCVDontStuckToBorder);
	title= new TextItem("");
	objList->SetId(cIdIClickList);
	objList->SetMinExtent(Point(150,0));
}

PeInspectClickDialog::~PeInspectClickDialog()
{
	SafeDelete(objList);
}

int PeInspectClickDialog::ShowInWindow(VObject *vop)
{
	objList->ClearSelection();
	LoadContainers(vop);
	return ShowOnWindow(vop->GetWindow());
}

void PeInspectClickDialog::LoadContainers(VObject *vop)
{
	GrFont fid= gFixedFont->Fid();
	int size= gFixedFont->Size();
	static Font *boldFont, *font;
	if (boldFont == 0)
		boldFont= new_Font(fid, size, eFaceBold);

	title->SetString(form("Clicked on %s (0x%x)", vop->ClassName(), (int)vop), TRUE);
	OrdCollection *oc= new OrdCollection;

	for (int i= 0; vop != 0; vop= vop->GetContainer(), i++) {
		if (i == 0)
			font= boldFont;
		else
			font= gFixedFont;

		oc->Add(MakeItem(vop, font));
	}
	objList->SetCollection(oc);
}

VObject *PeInspectClickDialog::MakeItem(VObject *vop, Font *fp)
{
	char buf[400];
	buf[0]= '\0';
	vop->InspectorId(buf, sizeof(buf));
	if (!strlen(buf))
		return new PeObjectItem(cIdNone,
					form("%s (0x%x)", vop->ClassName(), (int)vop), vop, fp);
	return new PeObjectItem(cIdNone, form("%s (%s)", vop->ClassName(), buf), vop, fp);
}

VObject *PeInspectClickDialog::DoMakeContent()
{
	return
		new Matte(
			new VExpander(2,
				title,
				new HExpander(10,
					new Scroller(objList, Point(200, 150), cIdIClickList),
					new VBox(gPoint5, (VObjAlign)(eVObjVGapExpand|eVObjHExpand),
						new ActionButton(cIdOk,         "Inspect", TRUE),
						new ActionButton(cIdIClickEdit, "Edit Source"),
						new ActionButton(cIdIClickTree, "Show Hierarchy"),
						new ActionButton(cIdIClickDocu, "Show Documentation"),
						new ActionButton (cIdCancel,    "Cancel"),
						0
					),
					0
				),
				0
			)
		);
}

void PeInspectClickDialog::DoSetup()
{
	bool empty= objList->GetSelection().IsEmpty();
	EnableItem(cIdOk, !empty);
	EnableItem(cIdIClickEdit, !empty);
	EnableItem(cIdIClickTree, !empty);
	EnableItem(cIdIClickDocu, FALSE);
}

Object *PeInspectClickDialog::GetObject()
{
	Rectangle r= objList->GetSelection();
	if (r.IsEmpty())
		return 0;
	PeObjectItem *oi= Guard(objList->GetCollection()->At(r.origin.y), PeObjectItem);
	return oi->GetObject();
}

void PeInspectClickDialog::Control(int id, int p, void *v)
{
	switch (id) {

	case cIdIClickList:
		if (p == cPartCollDoubleSelect) {
			Dialog::Control(cIdOk, cPartAction, v);
			return;
		}
		DoSetup();
		return;
		
	case cIdOk:
	case cIdIClickEdit:
	case cIdIClickTree:
	case cIdIClickDocu:
		break;
		
	case cIdCancel:
		objList->ClearSelection();
		break;

	default:
		break;
	}
	Dialog::Control(id, p, v);
}

